#include <cstdio>
#include <algorithm>

using namespace std;

const int MAX = 15;

struct person {
	char id[MAX+10];
	int in_h;
	int in_m;
	int in_s;
	int out_h;
	int out_m;
	int out_s;
}P[1000];

bool cmp_in(person a, person b) {
	if (a.in_h != b.in_h) return a.in_h < b.in_h;
	if (a.in_m != b.in_m) return a.in_m < b.in_m;
	return a.in_s < b.in_s;
}

bool cmp_out(person a, person b) {
	if (a.out_h != b.out_h) return a.out_h > b.out_h;
	if (a.out_m != b.out_m) return a.out_m > b.out_m;
	return a.out_s > b.out_s;
}

int main() {
	int M;
	scanf("%d", &M);
	for (int i = 0; i < M; i++) {
		scanf("%s %d:%d:%d %d:%d:%d", P[i].id, &P[i].in_h, &P[i].in_m, &P[i].in_s, &P[i].out_h, &P[i].out_m, &P[i].out_s);
	}

	sort(P, P+M, cmp_in);
	printf("%s", P[0].id);
	sort(P, P+M, cmp_out);
	printf(" %s\n", P[0].id);

	return 0;
} 
